# Maintainer: Martell Malone <MartellMalone@gmail.com

_realname=clang

pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-svn" "${MINGW_PACKAGE_PREFIX}-clang-analyzer-svn" "${MINGW_PACKAGE_PREFIX}-llvm-svn") # "${MINGW_PACKAGE_PREFIX}-compiler-rt-svn"
pkgver=51262.b335d96
pkgrel=1
pkgdesc="LC language family frontend for LLVM (mingw-w64)"
arch=('any')
url="http://llvm.org"
license=("custom:University of Illinois/NCSA Open Source License")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('staticlibs' 'strip')
source=(#"llvm"::"svn+http://llvm.org/svn/llvm-project/llvm/trunk"
    #"clang"::"svn+http://llvm.org/svn/llvm-project/cfe/trunk"
    #"rt"::"svn+http://llvm.org/svn/llvm-project/compiler-rt/trunk"
    #"testsuite"::"svn+http://llvm.org/svn/llvm-project/test-suite/trunk"
    "llvm"::"git+http://llvm.org/git/llvm.git"
    "clang"::"git+http://llvm.org/git/clang.git"
    "rt"::"git+http://llvm.org/git/compiler-rt.git"
    "testsuite"::"git+http://llvm.org/git/test-suite.git"
    #"libcxx"::"git+http://llvm.org/git/libcxx.git"
    clang-win64-seh.patch
    clang-mingw-driver.patch
    llvm-win64-exceptions.patch
)
md5sums=('SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'SKIP')

pkgver() {
  cd "$srcdir/clang"
  local ver="$(git rev-parse HEAD)"
  # printf "r%s" "$(git svn find-rev ${ver})"
  printf "%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd ${srcdir}/llvm
  patch -p1 -i ${srcdir}/llvm-win64-exceptions.patch
  
  cd ${srcdir}/clang
  patch -p1 -i ${srcdir}/clang-mingw-driver.patch
  patch -p1 -i ${srcdir}/clang-win64-seh.patch

  cd ${srcdir}
  mv ${srcdir}/clang ${srcdir}/llvm/tools/clang
  mv ${srcdir}/rt ${srcdir}/llvm/projects/compiler-rt
  mv ${srcdir}/testsuite ${srcdir}/llvm/projects/test-suite
  # mv ${srcdir}/libcxx ${srcdir}/llvm/projects/libcxx
}

build() {
  mkdir -p ${srcdir}/llvm/build-${MINGW_CHOST}
  cd ${srcdir}/llvm/build-${MINGW_CHOST}
  echo 'ac_cv_have_decl_strerror_s=${ac_cv_have_decl_strerror_s=no}' > config.cache

  # Include location of libffi headers in CPPFLAGS
  CPPFLAGS+=" $(pkg-config --cflags libffi)"
  
  ../configure \
    -C \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --with-sysroot=${MINGW_PREFIX} \
    --enable-targets=x86,x86_64,cpp \
    --enable-optimized \
    --disable-assertions \
    --disable-pthreads \
    --with-python=/usr/bin/python2 \
    --enable-libffi \
    --enable-docs
    #--enable-shared
    #--enable-embed-stdcxx
    #--enable-libcpp
    #--enable-cxx11
    #
    #--enable-ltdl-install
    #
    #--with-c-include-dir
    #--with-gcc-toolchain
    #--with-default-sysroot
    #--with-binutils-include
    #--with-bug-report-url
  make
}

check() {
  cd "${srcdir}/llvm"
  make check
}

package_clang(){
  pkgdesc="C language family frontend for LLVM"
  depends=("${MINGW_PACKAGE_PREFIX}-llvm-svn")
  provides=("${MINGW_PACKAGE_PREFIX}-clang")
  conflicts=("${MINGW_PACKAGE_PREFIX}-clang")
  cd ${srcdir}/llvm/tools/clang
  local ver="$(git rev-parse HEAD)"
  pkgver="r$(git svn find-rev ${ver})"
  
  cd "${srcdir}/llvm/build-${MINGW_CHOST}/tools/clang"
    make -j1 DESTDIR="$pkgdir" install
  rm -r ${pkgdir}${MINGW_PREFIX}/docs
}

# package_compiler-rt(){
  # cd ${srcdir}/compiler-rt
  # local ver="$(git rev-parse HEAD)"
  # pkgver="r$(git svn find-rev ${ver})"
# }

package_llvm(){
  pkgdesc="Low Level Virtual Machine"
  depends=("${MINGW_PACKAGE_PREFIX}-gcc") # "${MINGW_PACKAGE_PREFIX}-compiler-rt"
  provides=("${MINGW_PACKAGE_PREFIX}-llvm")
  conflicts=("${MINGW_PACKAGE_PREFIX}-llvm")
  cd ${srcdir}/llvm
  local ver="$(git rev-parse HEAD)"
  pkgver="r$(git svn find-rev ${ver})"
  
  mv "${srcdir}/llvm/tools/clang" "${srcdir}/clang"
  mv "${srcdir}/llvm/build-${MINGW_CHOST}/tools/clang" "${srcdir}/clang-${MINGW_CHOST}"
  cd "${srcdir}/llvm/build-${MINGW_CHOST}"
  make -j1 DESTDIR="$pkgdir" install
  mv "${srcdir}/clang" "${srcdir}/llvm/tools/clang"
  mv "${srcdir}/clang-${MINGW_CHOST}" "${srcdir}/llvm/build-${MINGW_CHOST}/tools/clang"
  rm -r ${pkgdir}${MINGW_PREFIX}/docs
  
  # Install CMake stuff
  install -d "${pkgdir}${MINGW_PREFIX}"/share/llvm/cmake/{modules,platforms}
  install -Dm644 $srcdir/llvm/cmake/modules/*.cmake "${pkgdir}${MINGW_PREFIX}"/share/llvm/cmake/modules/
  install -Dm644 $srcdir/llvm/cmake/platforms/*.cmake "${pkgdir}${MINGW_PREFIX}"/share/llvm/cmake/platforms/
}

package_clang-analyzer() {
  pkgdesc="A source code analysis framework"
  url="http://clang-analyzer.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-clang-svn" "${MINGW_PACKAGE_PREFIX}-python2")
  provides=("${MINGW_PACKAGE_PREFIX}-clang-analyzer")
  conflicts=("${MINGW_PACKAGE_PREFIX}-clang-analyzer")
  cd ${srcdir}/llvm/tools/clang
  local ver="$(git rev-parse HEAD)"
  pkgver="r$(git svn find-rev ${ver})"
  
  install -d ${pkgdir}${MINGW_PREFIX}/{bin,lib/clang-analyzer}
  for _tool in scan-{build,view}; do
    cp -r tools/$_tool ${pkgdir}${MINGW_PREFIX}/lib/clang-analyzer/
    ln -s ${pkgdir}${MINGW_PREFIX}/lib/clang-analyzer/$_tool/$_tool ${pkgdir}${MINGW_PREFIX}/bin/
  done

  # scan-build looks for clang within the same directory
  install -d ${pkgdir}${MINGW_PREFIX}/lib/clang-analyzer/scan-build/
  ln -s ${srcdir}/llvm/build-${MINGW_CHOST}/Release/bin/clang.exe ${pkgdir}${MINGW_PREFIX}/lib/clang-analyzer/scan-build/

  # Relocate man page
  install -d ${pkgdir}${MINGW_PREFIX}/share/man/man1
  mv ${pkgdir}${MINGW_PREFIX}/lib/clang-analyzer/scan-build/scan-build.1 \
    ${pkgdir}${MINGW_PREFIX}/share/man/man1/

  # Use Python 2
  #sed -i \
  #  -e 's|env python$|&2|' \
  #  -e 's|/usr/bin/python$|&2|' \
  #  "${pkgdir}${MINGW_PREFIX}/lib/clang-analyzer/scan-view/scan-view" \
  #  "${pkgdir}${MINGW_PREFIX}/lib/clang-analyzer/scan-build/set-xcode-analyzer"

  # Compile Python scripts
  python2 -m compileall "${pkgdir}${MINGW_PREFIX}/lib/clang-analyzer"
  python2 -O -m compileall "${pkgdir}${MINGW_PREFIX}/lib/clang-analyzer"
  install -Dm644 LICENSE.TXT "${pkgdir}${MINGW_PREFIX}/share/licenses/clang-analyzer/LICENSE"
}

package_mingw-w64-i686-clang-svn(){
  package_clang
}

package_mingw-w64-i686-clang-analyzer-svn(){
  package_clang-analyzer
}
# package_mingw-w64-i686-compiler-rt-svn(){
#   package_compiler-rt
# }

package_mingw-w64-i686-llvm-svn(){
  package_llvm
}

package_mingw-w64-x86_64-clang-svn(){
  package_clang
}

package_mingw-w64-x86_64-clang-analyzer-svn(){
  package_clang-analyzer
}

# package_mingw-w64-x86_64-compiler-rt-svn(){
#   package_compiler-rt
# }

package_mingw-w64-x86_64-llvm-svn(){
  package_llvm
}
